package cn.funeralobjects.dictms.model;

import cn.funeralobjects.dictms.entity.QEDictRelatePath;
import cn.funeralobjects.dictms.entity.QEDictValue;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.JPQLQueryFactory;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FuneralObjects
 * Create date: 2020/6/9 11:09 AM
 */
@Data
@Accessors(chain = true)
public class DictValuePathDto {
    private Integer id;
    private String code;
    private String value;
    private Integer pathId;
    private Integer pathParentId;
    private Short depth;
    private Integer relateId;

    private static final Expression<?>[] COLUMNS = new Expression[]{
            QEDictValue.eDictValue.id,
            QEDictValue.eDictValue.code,
            QEDictValue.eDictValue.value,
            QEDictRelatePath.eDictRelatePath.relate.id,
            QEDictRelatePath.eDictRelatePath.id,
            QEDictRelatePath.eDictRelatePath.parentId,
            QEDictRelatePath.eDictRelatePath.depth
    };

    public static JPQLQuery<Tuple> createSelect(JPQLQueryFactory jpqlQueryFactory, Integer existsDelSalt) {
        return jpqlQueryFactory.select(COLUMNS)
                .from(QEDictRelatePath.eDictRelatePath)
                .innerJoin(QEDictValue.eDictValue).on(QEDictValue.eDictValue.id.eq(QEDictRelatePath.eDictRelatePath.dictValue.id))
                .where(QEDictValue.eDictValue.delSalt.eq(existsDelSalt));
    }

    public static DictValuePathDto toModel(Tuple tuple) {
        return tuple == null ? null : new DictValuePathDto()
                .setId(tuple.get(QEDictValue.eDictValue.id))
                .setCode(tuple.get(QEDictValue.eDictValue.code))
                .setValue(tuple.get(QEDictValue.eDictValue.value))
                .setPathParentId(tuple.get(QEDictRelatePath.eDictRelatePath.parentId))
                .setPathId(tuple.get(QEDictRelatePath.eDictRelatePath.id))
                .setDepth(tuple.get(QEDictRelatePath.eDictRelatePath.depth))
                .setRelateId(tuple.get(QEDictRelatePath.eDictRelatePath.relate.id));
    }

    public static List<DictValuePathDto> toModel(List<Tuple> tuples) {
        return tuples.stream().map(DictValuePathDto::toModel).collect(Collectors.toList());
    }

}
